<?php

class RegisterController extends Controller
{
	public $layout="column2";
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the register page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xCCCCCC,
				'minLength'=>7
			),
		);
	}
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('deny', 
				'actions'=>array('index',),
				'users'=>array('@'),				
			),
		);
	}
	public function actionIndex()
	{
			$model=new User('register');
			$profile = new UserProfile('register');
			
			$profile->gender = 1;
			if(isset($_POST['User']) && isset($_POST['UserProfile'])) {
				$model->attributes=$_POST['User'];
				$profile->attributes=$_POST['UserProfile'];
				$validate1 = $model->validate();
				$validate2 = $profile->validate();
				$model->active=1;
				if($validate1 && $validate2){ 			
					if($_POST['User']['password']!='') {
						$model->password = md5($_POST['User']['password']);
					}					
					if($_POST['UserProfile']['dateofbirth']!='') {						 
						$profile->dateofbirth = strtotime($_POST['UserProfile']['dateofbirth']);		
					}
					if($model->save(false)) {
						$profile->userID = $model->ID;
						
						if(CUploadedFile::getInstanceByName('UserProfile[avatar]')) { 
							$avatar = new File();
							$photo = CUploadedFile::getInstanceByName('UserProfile[avatar]');
							$avatar->filename = $photo->getName();
				    	$avatar->filesaved = encodeFilename(time().'_'.$photo->getName());
				    	$avatar->filetype =  $photo->getType();
				    	$avatar->folder = 'avatar';
				    	$photo->saveAs('upload/avatar/'.$avatar->filesaved);
				    	$avatar->save();
				    	$profile->avatar = $avatar->ID;
				    	//thumbnail 
				    	$thumb = Yii::app()->image->load(Yii::getPathOfAlias("webroot")."/upload/avatar/".$avatar->filesaved);
	            $thumb->resize(70, 70);
	            $thumb->save(Yii::getPathOfAlias("webroot")."/upload/avatar/thumbs_70x70/".$avatar->filesaved);//save vao thumb
						}
						
						$profile->save(false);
						
						$roleID = Role::model()->findByAttributes(array('role'=>'user'))->ID;
						$user_role = new UserRole();
						$user_role->userID = $model->ID;
						$user_role->roleID = $roleID;
						$user_role->save(false);
						
						// insert user status
						$user_status = new UserStatus();
						$statusID = Status::model()->findByAttributes(array('status'=>'OFFLINE'))->statusID;
						$user_status->statusID =$statusID;
						$user_status->userID = $model->ID;
						$user_status->save(false);
						
						
						Yii::app()->user->setFlash('success',"Thank you for registration! We have sent you a separate email that contains your activation link.");
						$this->redirect(Yii::app()->homeUrl);
					}
				}
			}
			$this->render('index', array(
						'model'=>$model, 'profile'=>$profile,
			));
	}
	public function sendmail($model){
		//send email to active account
		/*$active_link = Yii::app()->baseUrl.'/site/active/id/' . md5($model->id);
		$user = User::model()->findByPk($model->id);
		$message_email = new YiiMailMessage;
	  $message_email->view = 'mail';
	  $message_email->setTo(array($user->username));
	  $message_email->setFrom(yii::app()->params['adminEmail']);
	  $message_email->setSubject('Welcome to elearning.southern-soft.com!');
	  $body_email = 'Hello ' . $user->username . '<br /><br />
           Thank you very much for joining our website; <br /><br />
          Please <a href="' . $active_link . '">Click here</a> to active your account.
          If you have any problem with this registration, please contact '.Yii::app()->params['adminEmail'].'. <br /><br />
          Thank you!';
		$message_email->setBody($body_email, 'text/html');
		Yii::app()->mail->send($message_email);
		*/
	}
}